﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Entity;
using System.Data;
using ClassLibrary.Data;

namespace ClassLibrary.DAO
{
    public class TblCustomersDAO
    {
        private DBService _db = null;

        public TblCustomersDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        //insert - update 

        public bool Insert_Update(TblCustomers user, int mode)
        {
            bool flag = false;
            try
            {
                int result = 0;
                result = _db.ExeSP_NonQuery("sp_CustomerInsUp",
                    new string[] { "@FirstName", "@LastName", "@Email", "@Password", "@IsAdmin", "@IdLanguage", "@Mode" },
                    new object[] { user.FName, user.LName, user.Email, user.Pass, user.IsSupperAdmin, user.Idlanguage, mode },
                    CommandType.StoredProcedure);

                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // check Email

        public bool CheckExits(string mail, int mode)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_CheckExists",
                    new string[] { "@Email", "@mode" },
                    new object[] { mail, mode },
                    CommandType.StoredProcedure);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public TblCustomers GetUser(string emailaddress, string pass)
        {
            TblCustomers user = new TblCustomers();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_Check",
                   new string[] { "@Email", "@Password" },
                   new object[] { emailaddress, pass },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    user.Email = emailaddress;
                    user.LName = dr["LName"].ToString();
                    user.FName = dr["FName"].ToString();
                    user.Pass = pass;
                    user.IsSupperAdmin = Convert.ToBoolean(dr["IsSupperAdmin"].ToString());
                    user.Idlanguage = Convert.ToInt32(dr["idlanguage"].ToString());
                    user.CusID = Convert.ToInt32(dr["CusID"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return user;
        }
    }
}
